<template>
    <div ref="chartBarRef">
        <el-card>
            <div class="flex justify-between">
                <div class="text-14px bold">{{ title }}</div>
                <div class="flex">
                    <customer-switch v-model:active="active" class="ml-20px" :list="list"
                        @change="handleSwitchChange"></customer-switch>
                </div>
            </div>
            <div class="chart-container" ref="chartRef">
            </div>
        </el-card>
    </div>
</template>
<script setup>
import { ref, computed, watch, onMounted, onActivated} from 'vue'
import * as echarts from 'echarts'
import { getPayNearlySeven } from '@/api/dashboard/sasspay.ts'
import { getEleRechargeNearlySeven } from '@/api/dashboard/eleSign.ts'

const props = defineProps({
    title: {
        type: String,
        default: '近七日销售趋势'
    },
    showLegend: {
        type: Boolean,
        default: false
    },
    type: {
        type: String,
        default: 'sale'
    },
    insureType: {
        type: String,
        default: 'eleSign'
    },
    queryData: {
        type: Object,
        default() {
            return {}
        }
    }
})

const active = ref(1)
const list = ref([
    {
        name: '按金额',
        value: 1
    },
    {
        name: '按笔数',
        value: 2
    },
])

const handleChange = () => {
    console.log(111)
}

const handleSwitchChange = () => {
    setOption()
}

const chartRef = ref(null)
const chartBarRef = ref(null)
let myChart = null

const seriesMap = {
    1: '销售金额',
    2: '销售笔数'
}

const createChartOption = (date, data) => {
    const chartOption =
    {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: date,
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#EAEAEF',
                    type: 'solid'
                },
            },
            axisLabel: {
                show: true,
                color: '#9497B1',
                margin: 20
            }
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, '100%'],
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
            axisLabel: {
                show: true,
                color: '#9497B1'
            }
        },
        grid: {
            left: 40,
            right: 40,
            top: 30,
            bottom: 30,
        },
        series: [
            {
                name: '',
                type: 'line',
                // symbol: 'none',
                showSymbol: false,
                sampling: 'lttb',
                smooth: true,
                itemStyle: {
                    color: 'rgba(12, 83, 252, 1)'
                },
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {
                            offset: 0,
                            color: 'rgba(12, 83, 252, 0.2)'
                        },
                        {
                            offset: 1,
                            color: 'rgba(12, 83, 252, 0)'
                        }
                    ])
                },
                data: data
            }
        ]
    }
    chartOption.series[0].name = seriesMap[active.value]
    if (props.showLegend) {
        chartOption.legend = {
            right: 0,
            top: 5
        }
    }
    return chartOption
}
let chartData = {}

const setOption = () => {
    const date = chartData.day
    let data = []
    if (active.value === 1) {
        data = chartData.money || chartData.moneyQuantity
    } else {
        data = chartData.people || chartData.payQuantity
    }
    myChart.setOption(createChartOption(date, data))
}

const getData = () => {
    if(props.insureType === 'eleSign') {
        getPayNearlySeven({
            ...props.queryData
        }).then(res => {
            chartData = res
            setOption()
        })
    } else if(props.insureType === 'dashboardEleSign') {
        getEleRechargeNearlySeven({
            ...props.queryData
        }).then(res => {
            chartData = res
            setOption()
        })
    }
}

const resize = () => {
    myChart.resize()
}


watch(() => props, (newval) => {
    console.log(props.insureType, 'propspropspropsprops')
    getData()
}, {
    immediate: true,
    deep: true
})

defineExpose({
    chartBarRef,
    resize,
    getData
})

onMounted(() => {
    myChart = echarts.init(chartRef.value)
    // getData()
    // setOption()
})
onActivated(() => {
    myChart = echarts.init(chartRef.value)
    // getData()
    // setOption()
})
</script>
<style lang="scss" scoped>
.chart-container {
    width: 100%;
    height: 240px;
}
</style>